using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//Includes pour la gestion de la physique
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;


namespace Watch_It
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        Personnage perso;
        Camera maCamera;
        Couloir monCouloir;
        World monMonde;
        bool collision;

        KeyboardState monClavier, monAncienClavier;
                
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            perso = new Personnage();
            perso.init();
            maCamera = new Camera();
            monCouloir = new Couloir();



            monClavier = new KeyboardState();
            monAncienClavier = new KeyboardState();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //chargement personage
            perso.Dessin = Content.Load<Model>("Models\\MinionIndy2");
            perso.AspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
            

            //Chargement Couloir
            monCouloir.Dessin = Content.Load<Model>("Models\\p1_wedge");
            monCouloir.AspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;

            //chargement camera
            maCamera.Position = new Vector3(0.0f, 20.0f, -50.0f);
            maCamera.Cible = perso.Position + new Vector3(0f, 25f, 0f);
            maCamera.Vue = Matrix.CreateLookAt(maCamera.Position, maCamera.Cible, Vector3.Up);
            maCamera.Perspective = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60F), perso.AspectRatio, 0.1f, 10000.0f);
//            perso.Position = new Vector3(perso.Position.X, -15, perso.Position.Z);

            
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            monClavier = Keyboard.GetState();
            perso.gestionEvenement(monClavier, monAncienClavier);
            collision = false;
            for(int i=0;i < perso.Dessin.Meshes.Count;i++)
            {
                BoundingSphere zoneDeCollision = perso.Dessin.Meshes[i].BoundingSphere;
                zoneDeCollision.Center += perso.Position;

                for(int j=0; j< monCouloir.Dessin.Meshes.Count;j++)
                {
                    BoundingSphere zoneDeCollision2 = monCouloir.Dessin.Meshes[j].BoundingSphere;
                    zoneDeCollision2.Center += monCouloir.Position;

                    if (zoneDeCollision.Intersects(zoneDeCollision2))
                    {
                        collision = true;
                    }
                    if(collision)
                    {
                        perso.Vitesse = new Vector3(0f, 0f, 0f);
                    }
                    else
                    {
                        perso.VitesseParDefaut();
                    }
                }

            }
            


            perso.Deplacer(gameTime);
            maCamera.Actualiser(perso.Position);



            monAncienClavier = monClavier;
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here


            //Dessin du personnage
            perso.Dessine(maCamera);
            monCouloir.Dessine(maCamera);

            base.Draw(gameTime);
        }
    }
}
